# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=subversion
pkgver=1.9.2
pkgrel=1
pkgdesc="A Modern Concurrent Version Control System"
arch=('i686' 'x86_64')
url="http://subversion.apache.org/"
license=('APACHE')
groups=('VCS')
depends=('libsqlite' 'file' 'libserf' 'libsasl')
makedepends=('python2' 'perl' 'swig' 'ruby' 'libsqlite-devel' 'libserf-devel' 'libsasl-devel')
optdepends=('bash-completion: for svn bash completion'
            'python2: for some hook scripts'
            'ruby: for some hook scripts')
provides=('svn')
options=('!makeflags' '!libtool' '!emptydirs')
noextract=("subversion-${pkgver}.tar.bz2")
source=(http://www.apache.org/dist/subversion/subversion-${pkgver}.tar.bz2{,.asc}
        svnclean
        01-make-build-directories.patch
        02-fix-linking.patch
        03-fix-linking-perl.patch
        04-fix-switch-tests.patch
        05-retry-loop.patch
        06-svnadmin-binary-mode.patch
        07-fix-path-tests.patch
        08-ruby-test-fix.patch
        09-wincrypt.patch
        10-perl-vendors.patch
        11-XFAIL-case-sensitive-move-tests.patch
        12-fix-svnlook-test.patch
        13-dso_open.patch
        14-fix_svnauthz_tests.patch
        15-XFAIL_case_sensitive_tree_conflict.patch
        16-fix-swig3.patch
        17-fix-test-link.patch
        18-fix-serf-config.patch
        19-remove-contrib-from-configure.patch
        subversion-1.9.1-msys2.patch
        remove-checking-symlink.patch
        90-use-copy-instead-symlink.patch)
md5sums=('0a7e55bb58fe77072f19e108a56b468b'
         'SKIP'
         '07ee0cb01bdf8468cb8d4c22c0d6bc04'
         'e5e1cdfc8a9a517ec26f53cf5b1092d1'
         'd5a64fa7b1ace1a170ee2de97de81763'
         '47a6d32d3381b9f1c92a078e011f9e3c'
         'c36191a793567eb7f8228e6ae8e35981'
         '03a0661dea16504bc3bd75abe5d30df5'
         '31104af7c81202c9ab9da1053ccbdf11'
         'd2f9597e99e75cf24bc14dc45863aa13'
         '5c9424fa51cb6b45de95d977e468809c'
         '92a940b8bc107dbf92e00de6ba5500d2'
         'ad1452557f17bb9f187fcccc016de37c'
         'a77cffd068c482ca398444ab0c47bbe0'
         '37d72c8e15b29372d0defa04b9660a8e'
         '678c88e7e11d6a0a94d92cd0c89dad9d'
         '017b144c3ed6da5108d7ffdbca4d0aa1'
         '40e2da64e71a91c3cfa034e002c81566'
         '662e8a845529ef82d90996ac27173fa4'
         '02981c812b6d5dfa558144ac9c08f978'
         '930c4d2b8e93f5c422db7a3be6f5ca93'
         '097e41e35870212a2dd1d0a40b72da79'
         '89b5ddbb3e49cedefc3f3187a124dd3e'
         'be151d5feeab94774c1366382044f821'
         'c02191c936e226cdbef25af9e5aac257')

PYTHON_SITELIB=/usr/lib/python2.7/site-packages

prepare() {
  cd $startdir/
  [ -d $srcdir/$pkgname-$pkgver ] && rm -rf -d $srcdir/$pkgname-$pkgver
  tar -xjvf ${SRCDEST}/${pkgname}-${pkgver}.tar.bz2 -C $srcdir || true

  cd $srcdir/$pkgname-$pkgver

  sed -i 's|/usr/bin/env python|/usr/bin/env python2|' \
    tools/hook-scripts/{,mailer/{,tests/}}*.py \
    tools/client-side/mergeinfo-sanitizer.py

  sed -i 's|/usr/bin/env python|/usr/bin/env python2|' gen-make.py build/getversion.py
  sed -i 's|except IOError,|except IOError as|' build/getversion.py

  patch -p1 -i ${srcdir}/01-make-build-directories.patch
  patch -p1 -i ${srcdir}/02-fix-linking.patch
  patch -p1 -i ${srcdir}/03-fix-linking-perl.patch
  patch -p1 -i ${srcdir}/04-fix-switch-tests.patch
  patch -p1 -i ${srcdir}/05-retry-loop.patch
  patch -p1 -i ${srcdir}/06-svnadmin-binary-mode.patch
  patch -p1 -i ${srcdir}/07-fix-path-tests.patch
  patch -p1 -i ${srcdir}/08-ruby-test-fix.patch
  patch -p1 -i ${srcdir}/09-wincrypt.patch
  patch -p1 -i ${srcdir}/10-perl-vendors.patch
  patch -p1 -i ${srcdir}/11-XFAIL-case-sensitive-move-tests.patch
  patch -p1 -i ${srcdir}/12-fix-svnlook-test.patch
  patch -p1 -i ${srcdir}/13-dso_open.patch
  patch -p1 -i ${srcdir}/14-fix_svnauthz_tests.patch
  patch -p1 -i ${srcdir}/15-XFAIL_case_sensitive_tree_conflict.patch
  patch -p1 -i ${srcdir}/16-fix-swig3.patch
  patch -p1 -i ${srcdir}/17-fix-test-link.patch
  patch -p1 -i ${srcdir}/18-fix-serf-config.patch
  patch -p1 -i ${srcdir}/19-remove-contrib-from-configure.patch
  patch -p1 -i ${srcdir}/subversion-1.9.1-msys2.patch
  patch -p1 -i ${srcdir}/remove-checking-symlink.patch
  patch -p1 -i ${srcdir}/90-use-copy-instead-symlink.patch

  PYTHON=/usr/bin/python2 ./autogen.sh
}

build() {
  cd ${pkgname}-${pkgver}
  export PYTHON=/usr/bin/python2
  ./configure --prefix=/usr \
      --build=${CHOST} \
      --with-apr=/usr \
      --with-apr-util=/usr \
      --with-zlib=/usr \
      --with-serf=/usr \
      --with-sqlite=/usr \
      --without-apxs \
      --without-gnome-keyring \
      --enable-shared --disable-static \
      --with-ruby-sitedir=/usr/lib/ruby/vendor_ruby \
      --disable-mod-activation \
      --enable-disallowing-of-undefined-references \
      --enable-local-library-preloading

  #make LT_LDFLAGS="-L$Fdestdir/usr/lib"
  # make swig_pydir=/usr/lib/python2.7/site-packages/libsvn \
  #  swig_pydir_extra=/usr/lib/python2.7/site-packages/svn swig-py swig-pl swig-rb

  make clean-swig
  make autogen-swig
  make all

  plain "Compiling swig bindings: ruby"
  make swig-rb

  plain "Compiling swig bindings: perl"
  make swig-pl

  plain "Compiling swig bindings: python"
  make swig-py swig_pydir=${PYTHON_SITELIB}/libsvn swig_pydir_extra=${PYTHON_SITELIB}/svn
}

#check() {
#   cd ${pkgname}-${pkgver}
#   export LANG=C LC_ALL=C
#   make check check-swig-pl check-swig-py check-swig-rb CLEANUP=yes # check-javahl
#}

package() {
  cd ${pkgname}-${pkgver}

  make -j1 DESTDIR="${pkgdir}" INSTALLDIRS=vendor \
    swig_pydir=${PYTHON_SITELIB}/libsvn \
    swig_pydir_extra=${PYTHON_SITELIB}/svn \
    install install-tools install-swig-py install-swig-pl install-swig-rb

  install -dm755 "${pkgdir}"/usr/share/subversion
  cp -a tools/hook-scripts "${pkgdir}"/usr/share/subversion/
  rm "${pkgdir}"/usr/share/subversion/hook-scripts/*.in

  ## svnclean
  install -Dm 755 ${srcdir}/svnclean "${pkgdir}"/usr/bin/svnclean

  install -Dm 644 tools/client-side/bash_completion \
     "${pkgdir}"/usr/share/bash-completion/completions/subversion
  for i in svn svnadmin svndumpfilter svnlook svnsync svnversion; do
      ln -sf subversion "${pkgdir}"/usr/share/bash-completion/completions/${i}
  done

  # Remove illegal files
  find ${pkgdir}/usr -type f -name "*::*" | xargs rm -f

  # fix permissons
  find ${pkgdir}/usr -type f \( -name *.dll -o -name *.exe \) | xargs chmod 0755
}
